import {reqUserTradeInfo, reqUserAddressList} from '@/api'

const state = {
    tradeInfo: {},
    userAddressList: []
}

const actions = {
    //获取用户要购买的商品数据
    async getTradeInfoData({commit}) {
        const result = await reqUserTradeInfo()
        if(result.code === 200) {
            commit('SAVE_USER_TRADE_DATA',result.data)
        } else {
            console.log(result.message)
        }
    },
    //获取用户的收获地址
    async getUserAddressListData({commit}) {
        const result = await reqUserAddressList()
        if(result.code === 200) {
            commit('SAVE_USER_ADDRESS_LIST', result.data)
        } else {
            console.log(result.message)
        }
    }


}

const mutations = {
    SAVE_USER_TRADE_DATA(state, payload) {
        state.tradeInfo = payload
    },

    SAVE_USER_ADDRESS_LIST(state, payload) {
        state.userAddressList = payload
    }
}

const getters = {
    // 商品列表
  detailArrayList(state) {
    return state.tradeInfo.detailArrayList
  },
  // 商品总金额
  originalTotalAmount(state) {
    return state.tradeInfo.originalTotalAmount
  },
  // 实付金额
  totalAmount(state) {
    return state.tradeInfo.totalAmount
  },
  // 商品数量
  totalNum(state) {
    return state.tradeInfo.totalNum
  },
  // 订单编号
  tradeNo(state) {
    return state.tradeInfo.tradeNo
  },
  activityReduceAmount(state) {
    return state.tradeInfo.activityReduceAmount
  }
}

export default {
    namespaced: true,
    state,
    actions,
    mutations,
    getters,
}